//
//  WebViewVC.m
//  ShareElectricity
//
//  Created by sunsoft on 2017/6/29.
//  Copyright © 2017年 sunnsoft.HH. All rights reserved.
//

#import "WebViewVC.h"

@interface WebViewVC ()<WKNavigationDelegate, WKUIDelegate,WKScriptMessageHandler>
@property(nonatomic,strong)UIProgressView * ProgressView;
@property (strong, nonatomic)  WKWebView *WK_Web;
@property (nonatomic,strong)NSBundle *bundle;

@end

@implementation WebViewVC

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.WK_Web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]]];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = GrayColor;
    [SEChangeLanguage initUserLanguage];
    _bundle = [SEChangeLanguage bundle];
    __weak typeof(self) weakSelf = self;
    if ([_Navtitle isEqualToString:@"用戶協議"]) {
        self.urlStr = @"/public/getDetail.aspf";
        [HNaviBar customNaviBarWithTitle:[_bundle localizedStringForKey:@"用戶協議" value:nil table:@"Localization"] andLeftEvent:^(UIButton *leftSender) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } aimView:self.view];
    }else if ([_Navtitle isEqualToString:@"關於我們"]){
        self.urlStr = @"/public/getDetail.aspf";
        [HNaviBar customNaviBarWithTitle:[_bundle localizedStringForKey:@"關於我們" value:nil table:@"Localization"] andLeftEvent:^(UIButton *leftSender) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } aimView:self.view];
    }else if ([_Navtitle isEqualToString:@"使用規則"]){
        self.urlStr = @"/public/getSettingList.aspf";
        [HNaviBar customNaviBarWithTitle:[_bundle localizedStringForKey:@"使用規則" value:nil table:@"Localization"] andLeftEvent:^(UIButton *leftSender) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } aimView:self.view];

    }else if ([_Navtitle isEqualToString:@"私隱政策"]){
        self.urlStr = @"/public/getDetail.aspf";
        [HNaviBar customNaviBarWithTitle:[_bundle localizedStringForKey:@"私隱政策" value:nil table:@"Localization"] andLeftEvent:^(UIButton *leftSender) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } aimView:self.view];

    }else if ([_Navtitle isEqualToString:@"支付指導"]){
        self.urlStr = @"/public/getDetail.aspf";
        [HNaviBar customNaviBarWithTitle:[_bundle localizedStringForKey:@"支付指導" value:nil table:@"Localization"] andLeftEvent:^(UIButton *leftSender) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } aimView:self.view];
        
    }else if ([_Navtitle isEqualToString:@"按金協議"]){
        self.urlStr = @"/public/getDetail.aspf";
        [HNaviBar customNaviBarWithTitle:[_bundle localizedStringForKey:@"按金協議" value:nil table:@"Localization"] andLeftEvent:^(UIButton *leftSender) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } aimView:self.view];
        
    }else if ([_Navtitle isEqualToString:@"充值協議"]){
        self.urlStr = @"/public/getDetail.aspf";
        [HNaviBar customNaviBarWithTitle:[_bundle localizedStringForKey:@"充值協議" value:nil table:@"Localization"] andLeftEvent:^(UIButton *leftSender) {
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } aimView:self.view];
        
    }
    if (!self.ProgressView) {
        self.ProgressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
        self.ProgressView.frame = CGRectMake(0,63, HHScreenWidth,2);
        //
        //设置进度条的色彩
        [self.ProgressView setTrackTintColor:[UIColor whiteColor]];
        self.ProgressView.progressTintColor = RGBColor(77, 191, 77);
        [self.view addSubview:self.ProgressView];
    }
    //配置信息
    WKWebViewConfiguration *config=[[WKWebViewConfiguration alloc]init];
    config.preferences=[[WKPreferences alloc]init];
    config.preferences.minimumFontSize = 10;
    config.preferences.javaScriptEnabled = true;
    // 默认是不能通过JS自动打开窗口的，必须通过用户交互才能打开
    config.preferences.javaScriptCanOpenWindowsAutomatically =true;
    // 通过js与webview内容交互配置
    config.userContentController=[[WKUserContentController alloc]init];
    // 添加一个名称，在JS通过这个名称发送消息
    [config.userContentController addScriptMessageHandler:self name:@"courseInfo"];
    
    
    self.WK_Web = [[WKWebView alloc] initWithFrame:CGRectMake(0, 64, HHScreenWidth, HHScreenHeight-64) configuration:config];
    [self.WK_Web sizeToFit];
    
    if ([_Navtitle isEqualToString:@"用戶協議"]) {
        
        if (_language == nil) {
            
            _urlStr = [NSString stringWithFormat:@"%@%@?title=%@&language=%@", SEBaseURLStr, self.urlStr,[self.Navtitle stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]],[SEUserData sharedInstance].user.languageStr];
        }else{
            
            _urlStr = [NSString stringWithFormat:@"%@%@?title=%@&language=%@", SEBaseURLStr, self.urlStr,[self.Navtitle stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]],_language];
        }
    }else{
        
        _urlStr = [NSString stringWithFormat:@"%@%@?title=%@&language=%@", SEBaseURLStr, self.urlStr,[self.Navtitle stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]],[SEUserData sharedInstance].user.languageStr];
    }
    HLog(@"%@",_urlStr);

    [self.WK_Web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_urlStr]]];
    self.WK_Web.navigationDelegate = self;
    [self.WK_Web addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
    [self.view addSubview:self.WK_Web];

    
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"didFailProvisionalNavigation");
}
- (void)dealloc{
    [self.WK_Web removeObserver:self forKeyPath:@"estimatedProgress"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"estimatedProgress"]) {
        
        if (object == self.WK_Web) {
            [self.ProgressView setAlpha:1.0f];
            [self.ProgressView setProgress:self.WK_Web.estimatedProgress animated:YES];
            
            if(self.WK_Web.estimatedProgress >= 1.0f) {
                
                [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                    [self.ProgressView setAlpha:0.0f];
                } completion:^(BOOL finished) {
                    [self.ProgressView setProgress:0.0f animated:NO];
                    self.ProgressView.hidden = YES;
                }];
                
            }
        }
        else
        {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
        
    }else {
        
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
